package com.week.tiny_video.service.impl;

import com.week.tiny_video.dao.ConcernDao;
import com.week.tiny_video.pojo.Concern;
import com.week.tiny_video.pojo.User;
import com.week.tiny_video.service.ConcernService;
import com.week.tiny_video.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ConcernServiceImpl implements ConcernService {

    @Autowired
    private ConcernDao concernDao;

    @Autowired
    private UserService userService;

    @Override
    public Concern get(int id) {
        return concernDao.findById(id).orElse(null);
    }

    @Override
    public void add(Concern concern) {
        concernDao.save(concern);
    }

    @Override
    public void update(Concern concern) {
        concernDao.save(concern);
    }

    @Override
    public void delete(int id) {
        concernDao.deleteById(id);
    }

    @Override
    public List<Concern> listByFans(int id) {
        User user = userService.getById(id);
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return concernDao.findAllByFans(user);
    }

    @Override
    public List<Concern> listByFollower(int id) {
        User user = userService.getById(id);
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return concernDao.findAllByFollowUser(user);
    }

    @Override
    public boolean isExist(int foId, int fansId) {
        Concern byFollowUserAndFans = getByBoth(foId,fansId);
        return null != byFollowUserAndFans;
    }

    @Override
    public Concern getByBoth(int foId, int fansId) {
        User fo = userService.getById(foId);
        User fans = userService.getById(fansId);
        Concern byFollowUserAndFans = concernDao.findByFollowUserAndFans(fo, fans);
        return byFollowUserAndFans;
    }
}
